package phi.engine.core
{
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2World;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	
	import phi.engine.PhiEngine;
	import phi.engine.scene.SceneManager;
	
	public class Game implements IGame
	{
		// Protected var
		protected var engine :PhiEngine = PhiEngine.getInstance();
		protected var gravity :b2Vec2 = new b2Vec2(0, 100);
		
		// Private var
		private var _window :DisplayObjectContainer;
		private var _sceneManager :SceneManager;
		private var _world :b2World;
		
		/**
		 * Constructor
		 * @param window
		 */
		public function Game( window:DisplayObjectContainer )
		{
			this._window = window;
		}

		// --------------------------------------------------------------------
		// Get/Set functions
		// --------------------------------------------------------------------
		
		public function get sceneManager():SceneManager
		{
			return _sceneManager;
		}

		public function get window():DisplayObjectContainer
		{
			return _window;
		}
		
		public function get world():b2World
		{
			return _world;
		}
		
		// --------------------------------------------------------------------
		// Public functions
		// --------------------------------------------------------------------
		
		public function init():void
		{
			// Create scene manager
			_sceneManager = new SceneManager();
			
			// Create physics world
			_world = new b2World( gravity, true );
			//_world.SetContactListener( new ContactListner() );
		}
		
		/**
		 * Update scene and all objects
		 * @param gameTime
		 */
		public function update( gameTime:GameTime ):void
		{
			// Update physics world
			world.Step( 1 / PhiEngine.FRAME_RATE, PhiEngine.STEPS, 0 );
			
			// Update scenes
			sceneManager.update( gameTime );
		}

		public function handleInput():void
		{
			sceneManager.handleInput()
		}
		
		/**
		 * Draw drawble actors
		 * @param gameTime
		 */
		public function draw( gameTime:GameTime ):void
		{
		}
	}
}